#!/bin/bash
scriptWorkd=$(cd "$(dirname $0)";pwd)

### ========= 全局变量 ===========
### ==============================

### ========= 前置文件 ===========
if [[ -f ${HOME}/bin/basic_func.sh ]];then  ### 存在才载入
    source ${HOME}/bin/basic_func.sh
elif [[ -f ${scriptWorkd}/../bin/basic_func.sh ]];then
    source ${scriptWorkd}/../bin/basic_func.sh
fi
### ==============================

function userSelect() {
set -e
    while true
    do
	    if [[ $# -le 1 ]]; then
	        tf=${tf:-"N"}	
        	else
	        tf=$2
	    fi
	    # 输出提示信息
        # echo -e "${normal}10秒倒计时"
        echo -e "${normal}10 seconds' countdown"
	    case $tf in 
	    [nN][oO]|[nN])
	        echo -e "${yellow}$1? [y/N] ${normal}"
	        ;;
	    [yY][eE][sS]|[yY])
	        echo -e "${yellow}$1? [Y/n] ${normal}"
	        ;;
        *)
	        tf="N"
	        echo -e "${yellow}$1? [y/N] ${normal}"
            ;;
	    esac
        set +e
    	read -t 10 confirm
    	confirm=${confirm:-${tf}}
        set -e
	# 实际判断
    	case "${confirm}" in 
	    [yY][eE][sS]|[yY])
		echo -e "${green}Starting${normal}"
		#exit 0
		break
		;;

	    [nN][oO]|[nN])
		echo -e "${red}Canceled${normal}"
		return 1
		;;

	     *)
		echo "${yellow}Invalid input...${normal}"
		;;
    	esac
    done
    sleep 2
    return 0
set +e
}

function install-addon-managers() {
set -e
    git config --global http.sslVerify false

    if ! [[ -d ~/.local/share/nvim/site/pack/packer/start/packer.nvim ]]; then
	    lastDir=$(pwd)
	    mkdir -p ~/.local/share/nvim/site/pack/packer/start/
	    cd ~/.local/share/nvim/site/pack/packer/start/
        echo -e "${blue1}安装packer插件管理器${normal}"
	    gitClone wbthomason/packer.nvim
	    cd ${lastDir}
    fi
    if ! [[ -d ~/.tmux/plugins/tpm ]]; then
	    lastDir=$(pwd)
	    mkdir -p ~/.tmux/plugins
	    cd ~/.tmux/plugins
        echo -e "${blue1}安装tpm插件管理器${normal}"
	    gitClone tmux-plugins/tpm
	    cd ${lastDir}
    fi

    ### 安装vim-plug
    if ! [[ -e "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim ]]; then
    	lastDir=$(pwd)
    	mkdir -p ~/.local/share/nvim/site/autoload/tmp
    	cd ~/.local/share/nvim/site/autoload/tmp
        echo -e "${blue1}安装vim-plug插件管理器${normal}"
    	gitClone junegunn/vim-plug
    	mv vim-plug/plug.vim ~/.local/share/nvim/site/autoload/
    	cd ${lastDir}
    	delete_dir ~/.local/share/nvim/site/autoload/tmp
        # sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
            # ${proxy}/https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
    fi
}

function pathCleaner() {
set -Ee

    ### 解决环境变量污染的问题
    sudo touch /etc/wsl.conf
    sudo chmod o+w /etc/wsl.conf
    echo -e "[interop]\nenabled=false\nappendWindowsPath=false" > /etc/wsl.conf
    sudo chmod o-w /etc/wsl.conf
    
    ### 解决mlocate会把Windows文件也搜索的问题
    if [[ -f /etc/updatedb.conf ]];then
        sudo sed -i '3c\PRUNEPATHS="/mnt /tmp /var/spool /media /var/lib/os-prober /var/lib/ceph /home/.ecryptfs /var/lib/schroot"' /etc/updatedb.conf
    else
        sudo cp ./.deps/etc/updatedb.conf /etc/updatedb.conf
    fi
set +Ee
}

